<?php  
session_start();  
ob_start(); 
require('mc_table.php');
require_once('../connections/con_gl.php'); 
$cabang = $_GET['cabang'];
$bulan = $_GET['bulan'];
$tahun = $_GET['tahun'];
global $num,$days,$first,$last;
$num =cal_days_in_month(CAL_GREGORIAN, $bulan, $tahun);
$first =  $tahun.'/'.$bulan.'/'.'01';
$last =  $tahun.'/'.$bulan.'/'.$num;

list($namacabang) = mysql_fetch_row(mysql_query("select gudang from gl_gudang where id='$cabang'"));
  
//instansiasi fpdf
$pdf = new PDF_MC_Table('L','cm','A3');
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(false);
$pdf->AddPage();
$pdf->SetMargins(0.7,0.7,0.7);  

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", 0, 1, 'C');
$pdf->Cell(41,2, "LAPORAN EFEKTIFITAS UNIT", 0, 1, 'C');
$pdf->Text(18.5,3.2, DateToIndo($bulan,$tahun));
$pdf->SetFont('Arial','B','12');
$pdf->SetXY(1.5,3);

function DateToIndo($bulan,$tahun) { // fungsi atau method untuk mengubah tanggal ke format indonesia
   // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
		$BulanIndo = array("JANUARI", "FEBRUARI", "MARET",
						   "APRIL", "MEI", "JUNI",
						   "JULI", "AGUSTUS", "SEPTEMBER",
						   "OKTOBER", "NOVEMBER", "DESEMBER");
	
		
		$result = $BulanIndo[(int)$bulan-1] . " ". $tahun;
		return($result);
}

$sql = "SELECT gudang FROM gl_gudang WHERE id = '$cabang' ";
$cekdata = mysql_query($sql) or die(mysql_error());
$row_data = mysql_fetch_array($cekdata);
$pdf->Cell(8,3, "CABANG : ".$row_data['gudang'],0,0,'L');


//HEADER TABLE
$pdf->SetFont('Arial','B',11);
$pdf->SetFillColor(125,125,125);
$pdf->SetTextColor(255,255,255);
$pdf->SetXY(1,5);
$pdf->Cell(1,0.7, "No",1,0,'C',true);
$pdf->Cell(2.8,0.7, "UNIT",1,0,'C',true);

		$days = array();
		for($i=1; $i<=$num; $i++)
		{
			$pdf->Cell(0.8,0.7, "$i",1,0,'C',true);
		
		}
		
$pdf->Cell(3.8,0.7, "CASHIN",1,0,'C',true);
$pdf->Cell(3.8,0.7, "CASHOUT",1,0,'C',true);
$pdf->Cell(3.8,0.7, "PPN",1,1,'C',true);


//ISI TABLE	
	global $datesArray,$query2,$arr;
	
	unset($datesArray); 	
	$datesArray[] = array();
	$arr[] = array();
	
	
	$sql = "SELECT a.kendaraan,a.booking_date FROM gl_book as a inner join gl_retailtrans as b on (a.no_ref = b.no_ref) WHERE a.booking_date BETWEEN '$first' AND '$last' ";
	$q = mysql_query($sql) or die (mysql_error());
			
	while($result = mysql_fetch_array($q)){
		
		$arrNoPol[] = $result['kendaraan'];
		$arrBookDate[] = $result['booking_date'];
			
	}	
	//Print_r($arrNoPol);
	//Print_r($arrBookDate);
	
	$query3 = mysql_query("SELECT gl_book.kendaraan, gl_book.booking_date, gl_retailtrans.supir, gl_retailtrans.bbm, gl_retailtrans.tol, gl_retailtrans.parkir, gl_retailtrans.akomod, gl_detailtrans.hargasatuan, gl_detailtrans.ppn
						   FROM gl_retailtrans, gl_book, gl_detailtrans WHERE gl_book.no_ref = gl_retailtrans.no_ref AND gl_detailtrans.no_ref = gl_book.no_ref AND gl_book.booking_date BETWEEN '$first' AND '$last' ") or die (mysql_error());
	while($hasil = mysql_fetch_array($query3)){
		$arrKendaraan[] = $hasil['kendaraan'];		
		$cashin[] = $hasil['hargasatuan'];
		$cashout[] = $hasil['supir']+$hasil['bbm']+$hasil['tol']+$hasil['parkir']+$hasil['akomod'];
		$ppn[] = $hasil['ppn'];
		$booking[] = $hasil['booking_date'];
	}
	//Print_r($booking);
	
	// disini bermain nya
			
		//D 1344 QE
		 
		 
		 global $q, $s;
		 $s = 0;
		 $ketemu = false;
		 $no = 1;
		 $max=30;//max baris perhalaman
		 $v=0;
		 
		 $query2 = mysql_query("SELECT DISTINCT a.nopolisi,a.tipekendaraan FROM gl_masterkendaraan as a inner join gl_tipekendaraan as b on (a.tipekendaraan = b.id) where a.cabang = $cabang") or die (mysql_error());
		  while(list($nopolisi,$tipekend) = mysql_fetch_row($query2)){
		  	//Print_r($nopolisi);
			$pdf->SetX(1);
			
			
			if($v == $max){
				
				
				$pdf->AddPage();
				$pdf->SetXY(1,3);
				$pdf->SetFont('Arial','B',11);
				$pdf->SetFillColor(125,125,125);
				$pdf->SetTextColor(255,255,255);
				
				$pdf->Cell(1,0.7, "No",1,0,'C',true);
				$pdf->Cell(2.8,0.7, "UNIT",1,0,'C',true);
				
						$days = array();
						for($i=1; $i<=$num; $i++)
						{
							$pdf->Cell(0.8,0.7, "$i",1,0,'C',true);
						
						}
						
				$pdf->Cell(3.8,0.7, "CASHIN",1,0,'C',true);
				$pdf->Cell(3.8,0.7, "CASHOUT",1,0,'C',true);
				$pdf->Cell(3.8,0.7, "PPN",1,1,'C',true);
				$v = 0;
				$pdf->SetX(1);
				
			}
			
			$pdf->SetTextColor(0,0,0);
			$pdf->SetFont('Arial','',9);
			$pdf->Cell(1,0.7, "$no",1,0,'C');
			$pdf->Cell(2.8,0.7, "$nopolisi",1,0,'C');
			
			
			//echo $num;
			for($i=1; $i<=$num; $i++)
			{
			   
			   if($i<=9)
			   {
					$days = '0'.$i;
			   }
			   else
			   {
				   $days = $i;
			   }
				
				$value = $tahun.'-'.$bulan.'-'.$days;
				//print_r($value);
				 
				
				
				if( searchday_in_array($nopolisi,$value,$arrBookDate,$arrNoPol) == 1)
				{
						$s++;
						$pdf->SetFillColor(210,90,90);
						$pdf->SetTextColor(255,255,255);
						$pdf->Cell(0.8,0.7, "1",1,0,'C',true);
						//echo "<td align='center' bgcolor='#00FF00'><span>1</span></td>";
					
				}else{
						$pdf->SetTextColor(0,0,0);
						$pdf->Cell(0.8,0.7, "0",1,0,'C');
						//echo "<td align ='center'><span>0</span></td>";
						
				}
				
				
				
					
			}
				
				//print_r($s);
				
				$d = searchCash($nopolisi,$arrKendaraan,$arrBookDate,$booking);
				
				if ($d[0] == 1)
				{	
					
					$t = $d[1];
					$total = $cashin[$t]*$s;
					$pdf->SetTextColor(255,255,255);
					$pdf->Cell(3.8,0.7, number_format($total,0,',','.').",-",1,0,'C',true);
					$pdf->Cell(3.8,0.7, number_format($cashout[$t],0,',','.').",-",1,0,'C',true);
					$pdf->Cell(3.8,0.7, number_format($ppn[$t],0,',','.').",-",1,1,'C',true);
					
				}else{
					$pdf->SetTextColor(0,0,0);
					$pdf->Cell(3.8,0.7, "0,-",1,0,'C');
					$pdf->Cell(3.8,0.7, "0,-",1,0,'C');
					$pdf->Cell(3.8,0.7, "0,-",1,1,'C');
				}
				
			  $s = 0;
			  $no++;
			  $v++;
		  }
		  
		  
		  
			function searchCash($nopolisi,$arrKendaraan,$arrBookDate,$booking){
			
			  $a = 0;
			  $b = 0;
			  $c = 0;
			  
			  
			  for ($i = 0; $i < count($arrKendaraan);$i++) {    
				for($y = 0; $y < count($booking);$y++){
				 if( $arrKendaraan[$i] == $nopolisi && $booking[$y] == $arrBookDate[$c] ) {
					$t = $i;
				    $a = 1;
					$c++;
					
					
				 } 
				}
				
			  }
			  
			  			  
			  return array($a,$t);
			}
		  
		  
			function searchday_in_array($nopolisi,$value, $arrBookDate,$arrNoPol){
			  
			  
			  $a = 0;
			  
			  for ($i = 0; $i < count($arrBookDate);$i++) {    
				  if($arrBookDate[$i] == $value && $arrNoPol[$i] == $nopolisi) {
					
				    $a=1;
				  } 
				 
			  }
			  return $a;
			}




$pdf->Output('Laporan Efektifitas Unit Cabang '.$row_data['gudang'].'Bulan '.DateToIndo($bulan,$tahun).'.pdf','D');

?>>